package global_village.gui;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import global_village.app.Utils;
import global_village.data.Group;
import global_village.data.ServerRequest;
import global_village.data.utils.AsyncHttpQuery;
import global_village.gui.utils.StatusListener;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import com.global_village.app.R;

public class CreateGroup extends Activity implements OnClickListener{

	private Button btSave;
	private Button btCancel;
	private EditText etGroup;
	private EditText etOwner;
	private Spinner spinner;

	private Group group;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.create_group);
		initGui();
		group = new Group();
	}

	private void initGui(){
		btSave = (Button) findViewById(R.id.create_group_save_bt);
		btCancel = (Button) findViewById(R.id.create_group_cancel_bt);

		etGroup = (EditText) findViewById(R.id.create_group_name_et);
		etOwner = (EditText) findViewById(R.id.create_group_owner_et);

		spinner = (Spinner) findViewById(R.id.create_group_logo_sp);
		addItemsOnSpinner();
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> parent, View v,
					int position, long id) {
				switch (position) {
				case 0:
					//TODO -Need to change - Do nothing just a header.
					
					break;

				case 1:
					group.setImg("pub.png");
					break;

				case 2:
					group.setImg("post_office.jpg");
					break;
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

		btSave.setOnClickListener(this);
		btCancel.setOnClickListener(this);

	}

	public void addItemsOnSpinner(){
		//TODO - HardCoded string need to change
		List<String> list = new ArrayList<String>();
		list.add("Pub");
		list.add("Post Office");
		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(dataAdapter);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.create_group_save_bt:
			group.setName(etGroup.getText().toString());
			//TODO - HardCoded need to change
			int pos = spinner.getSelectedItemPosition();
			Log.w("", "the pos is: "+ pos);
			if(pos == 0) group.setType("pub");
			else group.setType("post_office");

			AsyncHttpQuery task = new AsyncHttpQuery();
			task.setOnStatusListener(new StatusListener() {

				
				public void showStatus(String message) {
					//					d = new ProgressDialog(AppState.getCtx());
					//					d.setTitle(message);
					//					d.show();
				}

				public void processJson(String json) {
					Utils.showToast(json);
				}

				public void hideStatus() {
				}
			});
			//TODO need to change to configuration object
			//TODO HardCoded string.
			JSONObject json = new JSONObject();
			try{
				json.put("latitude", "24.3311081");
				json.put("longitude", "-78.33252");
			}catch (Exception e) {
				e.printStackTrace();
			}
			group.setLocation(json);
			//TODO HardCoded string.
			task.execute(new ServerRequest("POST", "http://" + Utils.getSettingsByKey("serverIp") + "/getData.inc.php", group.toString()));

			finish();

			break;

		case R.id.create_group_cancel_bt:
			finish();
			break;
		}
	}

}
